<template>
  <base-modal
    :title="$t('task.setting.taskTypeSetting.manage.createTaskType')"
    width="977px"
    @cancel="cancel"
    :show.sync="isShow"
    :mask-closeable="false">
    <div class="choose-trade">
      <el-row type="flex" class="choose-trade-nav">
        <bbx-tabs
          v-if="isShow"
          :value="industry"
          :tabs="industryNames"
          @choose="handleChooseIndustry"
        ></bbx-tabs>
      </el-row>
      <el-row class="trade-list" type="flex" justify="space-between">
        <el-row class="trade-item pointer apply" type="flex" v-for="item in industryItems" :key="item.taskTypeId" @click.native="selectTemplate(item)">
          <div class="trade-item-left" :style="{'background-color': getColor(item.taskTypeName)}">
            <i :class="['iconfont', getIcon(item.taskTypeName)]">
            </i>
          </div>
          <div class="trade-item-main">
            <el-row class="trade-item-name" type="flex">
              <h2>
                {{item.taskTypeNameTransfer || item.taskTypeName}}
              </h2>
              <div class="trade-item-tag-list">
                <div v-for="label in item.labels" :key="label" class="trade-item-tag">
                  {{label}}
                </div>
              </div>
            </el-row>
            <p v-html="item.description"></p>
            <el-row class="trad-item-operators">
              <el-button type="primary" size="small" class="trade-item-use">{{$t('common.base.use')}}</el-button>
            </el-row>
          </div>
        </el-row>
        <div v-if="!industryItems || !industryItems.length" class="flex flex-1 jus-center">
          <no-data-view-new></no-data-view-new>
        </div>
      </el-row>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="cancel">{{$t('common.base.steps.previous')}}</el-button>
    </div>
  </base-modal>
</template>

<script>
import * as SettingApi from '@src/api/SettingApi.ts';
import i18n from '@src/locales'
import BbxTabs from './BbxTabs.vue'
import noDataViewNew from '@src/component/common/NoDataViewNew/NoDataViewNew.vue'

let industryNames = [
  {
    label: i18n.t('task.setting.taskTypeSetting.manage.industryNames.name1'),
    value: '全部'
  },
  {
    label: i18n.t('task.setting.taskTypeSetting.manage.industryNames.name2'),
    value: '设备'
  },
  {
    label: i18n.t('task.setting.taskTypeSetting.manage.industryNames.name3'),
    value: 'IT服务'
  },
  {
    label: i18n.t('task.setting.taskTypeSetting.manage.industryNames.name4'),
    value: '家具建材'
  },
  {
    label: i18n.t('task.setting.taskTypeSetting.manage.industryNames.name5'),
    value: '电商'
  },
  {
    label: i18n.t('task.setting.taskTypeSetting.manage.industryNames.name6'),
    value: '机械'
  },
  {
    label: i18n.t('task.setting.taskTypeSetting.manage.industryNames.name7'),
    value: '上门服务'
  },
  {
    label: i18n.t('task.setting.taskTypeSetting.manage.industryNames.name8'),
    value: '公共服务'
  },
  {
    label: i18n.t('task.setting.taskTypeSetting.manage.industryNames.name9'),
    value: '其他'
  },
];
let iconMap = {
  '机械报修工单': {
    icon: 'icon-mechanics',
    color: '#3094d3'
  },
  '设备巡检保养': {
    icon: 'icon-maintain',
    color: '#1db6c5'
  },
  '设备交机培训': {
    icon: 'icon-train',
    color: '#3ba3da'
  },
  '办公设备工单': {
    icon: 'icon-work',
    color: '#1db6c5'
  },
  '医疗器械工单': {
    icon: 'icon-medicalcare',
    color: '#29b7ec'
  },
  '实施服务单': {
    icon: 'icon-implement',
    color: '#1db6c5'
  },
  '客户回访单': {
    icon: 'icon-tel',
    color: '#1db6c5'
  },
  '电商售后工单': {
    icon: 'icon-service1',
    color: '#f4c21d'
  },
  '家电维修': {
    icon: 'icon-appliance',
    color: '#3ba3da'
  },
  '电商退换货单': {
    icon: 'icon-refundable',
    color: '#f4c21d'
  },
  '内部报修': {
    icon: 'icon-inside',
    color: '#1db6c5'
  },
  '水电煤报修': {
    icon: 'icon-water',
    color: '#29b7ec'
  },
  '家居建材工单': {
    icon: 'icon-furniture',
    color: '#29b7ec'
  },
  '上门测量': {
    icon: 'icon-measure',
    color: '#52c0d1'
  },
  '设备维修': {
    icon: 'icon-equipment',
    color: '#41b8a3'
  }
}

export default {
  name: 'choose-trade-dialog',
  components: {
    BbxTabs,
    noDataViewNew,
  },
  props: {
    visiable: {
      type: Boolean,
      default: false
    },
  },
  computed: {
    industryNames() {
      return industryNames;
    },
    industryItems(){
      let industry = this.industry;
      if(industry == '全部') industry = null;

      return this.whole.filter(function(item){
        return industry == null || item.professions.indexOf(industry) >= 0;
      })
    }
  },
  data() {
    return {
      industry: '全部',
      whole: [], // 全部数据
      isShow: false
    }
  },
  watch: {
    visiable(newVal){
      this.isShow = newVal;
    },
  },
  methods: {
    handleChooseIndustry(item){
      this.industry = item.value
    },
    getIcon(key){
      let o = iconMap[key] || {};
      return o.icon
    },
    getColor(key){
      let o = iconMap[key] || {};
      return o.color
    },
    fetchSysTaskTypeList() {
      SettingApi.getSysTaskTypeList().then(res => {
        this.whole = res.data;
      }).catch(e => {
        console.error('fetchSysTaskTypeList => error', e);
      })
    },
    selectTemplate({taskTypeId, taskTypeNameTransfer, taskTypeName}) {
      this.$emit('select', {
        taskTypeId,
        typeName: taskTypeNameTransfer || taskTypeName
      });
      this.cancel();
    },
    cancel() {
      this.$emit('update:visiable', false);
    },
  },
  mounted() {
    this.fetchSysTaskTypeList();
  }
}
</script>

<style lang="scss" scoped>
.choose-trade{
    padding: 24px;
    .trade-list{
        flex-wrap: wrap;
        justify-content: space-between;
        .trade-item{
            position: relative;
            width: calc(50% - 6px);
            padding: 24px;
            margin-top: 12px;
            background: #FFFFFF;
            box-shadow: 0px 4px 12px 0px rgba(0, 0, 0, 0.1);
            border-radius: 4px;
            border: 2px solid transparent;
            .trade-item-left{
                display: flex;
                justify-content: center;
                align-items: center;
                i{
                    font-size: 24px;
                    color: #FFFFFF;
                }
            }
            &:hover{
                box-shadow: none;
                border: 2px solid $color-primary-light-6;
            }
            &:nth-child(2n){
                margin-right: 0;
            }
            &-left{
                width: 32px;
                min-width: 32px;
                height: 32px;
                background: $color-primary-light-3;
                border-radius: 2px;
                margin-right: 8px;
            }
            &-main{
                width: 375px;
                .trade-item-name {
                  flex: 1;
                  flex-direction: column;
                }
                h2{
                    @include text-ellipsis();
                    font-size: 14px;
                    line-height: 32px;
                    margin: 0;
                }
                .trade-item-tag{
                    margin-top: 8px;
                    height: 22px;
                    line-height: 22px;
                    padding: 0 8px;
                    font-size: 12px;
                    background: rgba(250, 140, 22, 0.2);
                    border-radius: 11px;
                    font-weight: 500;
                    color: #FA8C16;
                    border: 1px solid rgba(250, 140, 22, 0.2);
                    text-align: center;
                    display: inline-block;
                    margin-right: 8px;
                }
                p{
                    margin: 12px 0 0 0;
                    color:#666666;
                    white-space:pre-line;
                    font-size:12px;
                    line-height: 20px;
                    height: 85px;
                    overflow-y: auto;
                }
                .trad-item-operators {
                  margin-top: 12px;
                  .el-button {
                    margin-right: 12px;
                  }
                }
            }
        }
    }
}

.pointer{
    cursor: pointer;
}
</style>